package tools;

import com.google.gson.Gson;
import com.myk.game.gobangchess.constants.PathCost;
import com.myk.game.gobangchess.utils.DateTimeUtil;
import com.myk.game.gobangchess.utils.FileUtils;
import com.myk.game.gobangchess.utils.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class LoginMap2Json {


    //输出最近30天登录人数统计图
    public static void main(String[] args) throws IOException {
        List<LoginDateCount> loginData = new java.util.ArrayList<>();
        for (File file : FileUtils.listFilesInDir(PathCost.GLOBAL_LOGINS_DATA_DIR)) {
            LoginDateCount dateLoginCount = new LoginDateCount();
            String date = file.getName().replace(".txt", "");
            dateLoginCount.setDate(date);
            String names = FileUtils.readFile2String(file, "GBK");
            if (!StringUtils.isSpace(names)){
                dateLoginCount.setNameList(java.util.Arrays.asList(names.split("，")));
            }
            loginData.add(dateLoginCount);
        }
        String json = new Gson().toJson(loginData);
        String outputFile = PathCost.TOOL_OUTPUT_DIR + "逆界登录统计数据.json";
        FileUtils.writeFileFromString(outputFile, json);


        // 统计登录次数
        Map<String, Integer> loginCounts = new HashMap<>();
        for (LoginDateCount login : loginData) {
            for (String name : login.getNameList()) {
                loginCounts.merge(name, 1, Integer::sum);
            }
        }

        // 找出登录次数最多的前10个用户
        List<Map.Entry<String, Integer>> top10Logins = loginCounts.entrySet().stream()
                .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
                .limit(50)
                .collect(Collectors.toList());
        System.out.println("登录次数最多的前50个用户:");
        top10Logins.forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue()));

        // 统计连续登录天数
        Map<String, Integer> maxConsecutiveDays = new HashMap<>();
        for (String name : loginCounts.keySet()) {
            int consecutiveDays = 0;
            boolean isConsecutive = false;
            for (int i = 0; i < loginData.size(); i++) {
                if (loginData.get(i).getNameList().contains(name)) {
                    if (isConsecutive) {
                        consecutiveDays++;
                    } else {
                        consecutiveDays = 1;
                        isConsecutive = true;
                    }
                } else {
                    isConsecutive = false;
                }
                maxConsecutiveDays.put(name, Math.max(maxConsecutiveDays.getOrDefault(name, 0), consecutiveDays));
            }
        }

        // 找出连续登录次数最多的前10个用户
        List<Map.Entry<String, Integer>> top10Consecutive = maxConsecutiveDays.entrySet().stream()
                .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
                .limit(50)
                .collect(Collectors.toList());
        System.out.println("连续登录次数最多的前50个用户:");
        top10Consecutive.forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue()));
    }

    public static class LoginDateCount{
        private String date; //日期
        private List<String> nameList; //用户列表

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public List<String> getNameList() {
            return nameList;
        }

        public void setNameList(List<String> nameList) {
            this.nameList = nameList;
        }
    }
}
